﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using CDMS.Common;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors.Controls;
using CDMS.BLL;
using System.Configuration;
using DevExpress.LookAndFeel;

namespace CDMS
{
    public partial class ucProductList : DevExpress.XtraEditors.XtraUserControl
    {
        int productID = 0;
        string caption;
        int productGroupID = 0;
        public ucProductList(string caption)
        {
            InitializeComponent();
            this.caption = caption;

            treeList1.OptionsSelection.InvertSelection = true;
            LoadTreeList_ProductGroup();


            AddColumn();
            LoadData();
        }


        void LoadTreeList_ProductGroup()
        {
            // Look and feel
            treeList1.LookAndFeel.UseDefaultLookAndFeel = false;
            treeList1.LookAndFeel.SkinName = "iMaginary";

            treeList1.OptionsBehavior.Editable = false;
            treeList1.OptionsView.ShowColumns = false;
            treeList1.OptionsView.ShowIndicator = false;
            treeList1.OptionsView.ShowHorzLines = false;
            treeList1.OptionsView.ShowVertLines = false;
            //treeList1.OptionsView.ShowRoot = false;

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.ProductGroup);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["ProductGroup_ID"] = 0;
            dr["ProductGroup_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);

            treeList1.DataSource = dt;
            treeList1.KeyFieldName = "ProductGroup_ID";
            treeList1.ParentFieldName = "ProductGroup_ID";
            treeList1.OptionsBehavior.PopulateServiceColumns = true;
            treeList1.PopulateColumns();
            treeList1.BestFitColumns();
            treeList1.ExpandAll();

            treeList1.Columns["ProductGroup_ID"].Visible = false;
            treeList1.Columns["ProductGroup_Activated"].Visible = false;
            treeList1.Columns["ProductGroup_Note"].Visible = false;

            treeList1.FocusedNodeChanged += treeList1_FocusedNodeChanged;

        }

        void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node == null)
                return;
            string sNodename = e.Node.GetDisplayText("ProductGroup_ID");
            productGroupID = Convert.ToInt32(e.Node.GetValue("ProductGroup_ID"));
            LoadData();
        }
        void LoadLookUpEdit_ProductGroup()
        {
            /*
                DataTable dt = new DataTable();
                Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.ProductGroup);
                dt = category_BLL.GetALLActivated();
                DataRow dr = dt.NewRow();
                dr["ProductGroup_ID"] = 0;
                dr["ProductGroup_Name"] = "Tất cả";
                dt.Rows.InsertAt(dr,0);              
                lookUpEdit1.Properties.DataSource = dt;
                lookUpEdit1.Properties.DisplayMember = "ProductGroup_Name";
                lookUpEdit1.Properties.ValueMember = "ProductGroup_ID";

                // Thêm thông tin hai cột
                LookUpColumnInfoCollection coll = lookUpEdit1.Properties.Columns;
                coll.Add(new LookUpColumnInfo("ProductGroup_ID", 0, "ID"));
                coll.Add(new LookUpColumnInfo("ProductGroup_Name", 0, "Tên nhóm sản phẩm"));

                lookUpEdit1.Properties.Columns[0].Visible = false;
                lookUpEdit1.Properties.DropDownRows = dt.Rows.Count <= 7 ? 7 : 20;

                //  Set column widths according to their contents and resize the popup, if required.
                lookUpEdit1.Properties.BestFitMode = BestFitMode.BestFitResizePopup;

                // Enable auto completion search mode.
                lookUpEdit1.Properties.SearchMode = SearchMode.AutoComplete;
                // Specify the column against which to perform the search.
                lookUpEdit1.Properties.AutoSearchColumnIndex = 1;
         */
        }

      
        private void AddColumn()
        {
            gridView1.Columns.Clear();

            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_ID", "", "", 0, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_Code", "Mã sản phẩm", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_Name", "Tên sản phẩm", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Unit_Name", "ĐVT", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_RetailPrice", "Giá bán lẻ", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_WholesalePrice", "Giá bán buôn", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_ImportPrice", "Giá nhập", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_GuaranteePeriod", "Bảo hành", "", 0, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Product_Remind", "Nhắc việc", "", 0, true, true);
        }


        void LoadData()
        {
            FormGlobals.ShowWaitForm();
            Product_BLL product_BLL = new Product_BLL();
            DataTable dt = product_BLL.GetAllByProductGroupID(productGroupID);
            gridControl1.DataSource = dt;
            gridControl1.ForceInitialize();
            FormGlobals.CloseWaitForm();
        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItemEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
        }

        private void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            if (FormGlobals.Message_QuestionYesNo("Bạn có chắc chắn muốn xóa dòng này?", "Xác nhận xóa") == DialogResult.Yes)
            {

                productID = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
                Product_BLL product_BLL = new Product_BLL();
                if (product_BLL.Delete(productID))
                    LoadData();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            FormGlobals.ExportDataToExcel(gridControl1, caption);
        }

        private void ucProductList_BackColorChanged(object sender, EventArgs e)
        {
            
        }
    }
}
